//
//  SwitchView.swift
//  onemeetingmac
//
//  Created by 张衡 on 2020/6/3.
//  Copyright © 2020 onemeeting. All rights reserved.
//

import Foundation

class SwitchView: NSView {
    
    var isEnble:Bool = false
    var connerBtns:[ZHButton] = []
    var groups:[KIconGroup] = [
        KIconGroup.init(title: "画廊", image: "icon_meeting_gird"),
        KIconGroup.init(title: "焦点", image: "icon_meeting_speaker")
    ]

    let btnWidth:CGFloat = 42
    let titleHeight:CGFloat = 18
    
    
    var selectInx:Int = 0 {
        didSet {
            if selectInx > self.connerBtns.count-1 {
                return
            }
            self.selectView.snp.updateConstraints { (make) in
                make.left.equalTo(CGFloat(selectInx)*btnWidth)
            }
        }
    }
    
    
    var selectAction:((Int,ZHButton)->())?
    
    override init(frame frameRect: NSRect) {
        super.init(frame: frameRect)
        self.setupUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func getWidth() -> CGFloat {
        return btnWidth*CGFloat(groups.count)
    }
    
    func setupUI() {
        
        
        self.addSubview(conner)
        conner.snp.makeConstraints { (make) in
            make.top.width.centerX.equalToSuperview()
            make.height.equalToSuperview().offset(-titleHeight)
        }
        
        conner.addSubview(selectView)
        selectView.snp.makeConstraints { (make) in
            make.width.equalTo(btnWidth)
            make.height.equalToSuperview()
            make.centerY.equalToSuperview()
            make.left.equalTo(0)
        }
        self.addSubview(tConner)
        tConner.snp.makeConstraints { (make) in
            make.bottom.width.centerX.equalToSuperview()
            make.height.equalTo(titleHeight)
        }
        
        let w = self.initViews(inView: conner,tView: tConner)
        self.connerBtns = w.0
    }
    lazy var selectView: NSView = {
        let view = NSView()
        view.wantsLayer = true
        view.layer?.backgroundColor = color_gray(80).cgColor
        return view
    }()
    
    func initViews(inView:NSView,tView:NSView) -> ([ZHButton],[NSTextField]) {
        var btns:[ZHButton] = []
        var titles:[NSTextField] = []
        for (inx,val) in groups.enumerated() {
            let btn = ZHButton()
            btn.normalImage = NSImage.image(name:val.image)!
            btn.tag = inx+100
            btn.target = self
            btn.action = #selector(switchAction(senber:))
            inView.addSubview(btn)
            btns.append(btn)
            btn.snp.makeConstraints { (make) in
                make.width.equalTo(btnWidth)
                make.height.equalToSuperview()
                make.centerY.equalToSuperview()
                if inx==0 {
                    make.left.equalTo(0)
                }else {
                    make.left.equalTo(btns[inx-1].snp.right)
                }
            }
            
            let title = NSLabel.label(title: val.title!, textColor: color_gray(180), font: NSFont.systemFont(ofSize: 11))
            title.alignment = .center
            tView.addSubview(title)
            titles.append(title)
            title.snp.makeConstraints { (make) in
                make.width.equalToSuperview()
                make.height.equalTo(14)
                make.centerY.equalToSuperview()
                make.centerX.equalTo(btn)
            }
            
        }
        return (btns,titles)
    }
    
    lazy var conner: NSView = {
        let view = NSView()
        view.wantsLayer = true
        view.layer?.backgroundColor = color_gray(40).cgColor
        view.layer?.cornerRadius = 5
        view.layer?.masksToBounds = true
        return view
    }()
    
    lazy var tConner: NSView = {
        let view = NSView()
        return view
    }()
}

extension SwitchView {
    
    func select(senber:ZHButton) {
        let tag = senber.tag-100
        self.selectInx = tag
    }
    
    @objc func switchAction(senber:ZHButton) {
        if self.selectAction != nil {
            let tag = senber.tag-100
            self.selectAction!(tag,senber)
        }
    }
}
